<template>
    <input-select :id="id" :styles="styles" :options="options" :index="index" isChange="Y"  :data="data"  @input="changeData" :exkey="exkey_"></input-select>
</template>
<script>
import InputSelect  from '../input/input-select'
export default {
    props:['id','styles','value','data','index','appid','exkey'],
    name: "gb-select",
    components: {
        InputSelect
    },
     data(){
        return{
            options:[],
            lt_dat1:"",
            lt_dat2:"",
            lt_ex3_old:"",
            exkey_:{'label':'lt_ex3'},
            nameType:"",
            isFirstComplete:"N",
            pId:"",
            sz_ex29:""
        }
    },
    created(){
        this.$post("bygex","/byg/funeral/getLtSelect",{type:'礼厅'}).then(data=>{
            this.options=[];
            let data_={};
            data_.optname="";
            data_.optvalue="";
            data_.sf_id="";
            this.options.push(data_)
            for(let i=1;i<data.length;i++){
               // data[i].optvalue=data[i].sf_id;
                this.options.push(data[i])
            }
            
        })
        if(this.exkey&&this.exkey.nameType){
            this.nameType=this.exkey.nameType;
        }
        try{
            this.sz_ex29=this.data["szInfo"].sz_ex29
        }catch(e){}
        //alert(this.nameType)
        try{
            if(this.exkey&&this.exkey.isFirstComplete){
                this.isFirstComplete=this.exkey.isFirstComplete;
            }
        }catch(e){}
        let ids=this.id.split(".");
        let pId=ids[0];
        this.pId=pId;
        if(this.index)
            this.lt_ex3_old=this.data[pId][this.index]["lt_ex3"];
        else{
            this.lt_ex3_old=this.data[pId]["lt_ex3"];
        }


               
        let lt_dat1="";
        let lt_dat2="";
        let lt_ex3="";
        if(this.index||this.index=='0'){
                lt_dat1=this.data[pId][this.index]["lt_dat1"];
                lt_dat2=this.data[pId][this.index]["lt_dat2"];
                lt_ex3=this.data[pId][this.index]["lt_ex3"];
        }else{
            lt_dat1=this.data[pId]["lt_dat1"];
                lt_dat2=this.data[pId]["lt_dat2"];
                lt_ex3=this.data[pId]["lt_ex3"];
        }
        this.lt_dat1=lt_dat1;
        this.lt_dat2=lt_dat2;
        let szi_ex4_=0;
        for(let i=0;i<this.data["item"].length;i++){
            let szi_ex14=this.data["item"][i].szi_ex14;
            if(!szi_ex14){
                szi_ex14="";
            }
           // console.log(this.data["item"][i].szi_ex1+"=="+szi_ex14+"=="+this.nameType,"nameType==")
            if(this.data["item"][i].szi_ex1==lt_ex3&&szi_ex14==this.nameType){
                szi_ex4_=Number(this.data["item"][i].szi_ex4);
            }
        }
        let sz_ex25="";
        try{
            sz_ex25=this.data["szInfo"]["sz_ex25"];
        }catch(e){
            sz_ex25=this.data["sz"]["sz_ex25"];
        }
      //  alert(sz_ex25)
        if(lt_dat1&&lt_dat2&&(szi_ex4_<=1||(sz_ex25!="A"&&sz_ex25!="C"))){ 
          //  alert(2)
            this.changeLt(lt_dat1,lt_dat2,lt_ex3)
        }
       
        if(this.isFirstComplete=='Y'){
            this.changeLt(lt_dat1,lt_dat2,lt_ex3)
            this.isFirstComplete="N"
        }

       
    },
    watch:{
         data:{
            handler: function (newVal) {
                let ids=this.id.split(".");
                let pId=ids[0];
               
                let lt_dat1="";
                let lt_dat2="";
                let lt_ex3="";
                if(this.index||(this.index=='0'||this.index==0)){
                     lt_dat1=this.data[pId][this.index]["lt_dat1"];
                     lt_dat2=this.data[pId][this.index]["lt_dat2"];
                     lt_ex3=this.data[pId][this.index]["lt_ex3"];
                }else{
                    lt_dat1=this.data[pId]["lt_dat1"];
                     lt_dat2=this.data[pId]["lt_dat2"];
                     lt_ex3=this.data[pId]["lt_ex3"];
                }
                console.log("this.lt_ex3==="+lt_ex3+"=="+this.lt_ex3_old)
                if(lt_ex3!=this.lt_ex3_old){
                    this.lt_ex3_old=lt_ex3;
                     for(let i=0;i<this.options.length;i++){
                        let sf_id=this.options[i].sf_id;
                        let optname=this.options[i].optname;
                        if(optname==lt_ex3){
                            if(this.index)
                                this.$set(this.data[pId][this.index],"lt_ex4",sf_id);
                            else
                                 this.$set(this.data[pId],"lt_ex4",sf_id);
                        }
                    }
                    console.log("激怒--------------------")
                    if(lt_ex3){
                        console.log(this.exkey,"this.exkeysss")
                        //try{
                            if(this.exkey&&this.exkey.setDate){
                                let addType=this.exkey.setDate.addType;
                                let addNum=this.exkey.setDate.addNum;
                                if(!lt_dat1){
                                   /* let now = new Date();
                                    let date = now.toLocaleString().split(",")[0];  // 获取日期部分
                                    let dates=date.split("/");
                                    date=dates.join("-")
                                    console.log("date==="+date)*/

                                    let now = new Date();
                                    

                                    if(addType=="小时"){
                                        now=new Date(now.setHours(now.getHours()+Number(addNum)));
                                    }else if(addType=="分钟"){
                                        
                                        now=new Date(now.setMinutes(now.getMinutes()+Number(addNum)));
                                    }

                                    let year = now.getFullYear();
                                    let month = now.getMonth() + 1;
                                    let day = now.getDate();
                                    let hour = now.getHours();
                                    let minute = now.getMinutes();
                                    let second = now.getSeconds();
                                    month=month<10 ? "0"+month:month;
                                    day=day<10 ? "0"+day:day;
                                    hour=hour<10 ? "0"+hour:hour;
                                    minute=minute<10 ? "0"+minute:minute;
                                    second=second<10 ? "0"+second:second;
                                    let days=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second
                                    //alert(days);
                                    if(this.index||(this.index=='0'||this.index==0)){
                                        this.$set(this.data[pId][this.index],"lt_dat1",days);
                                       
                                    }else{
                                      
                                        this.$set(this.data[pId],"lt_dat1",days);
                                    }
                                    console.log("days==="+days)
                                }
                            }
                       // }catch(e){}
                    }
                }
              
               
                //alert(lt_dat1+"="+this.lt_dat1+"="+lt_dat2+"="+this.lt_dat2)
                if(lt_dat1!=this.lt_dat1||lt_dat2!=this.lt_dat2){
                    //alert(2)
                    this.changeLt(lt_dat1,lt_dat2,lt_ex3)
                }
                
                console.log("isFirstComplete====="+this.isFirstComplete+"=="+lt_ex3)
                if(this.isFirstComplete=='Y'){
                    this.changeLt(lt_dat1,lt_dat2,lt_ex3)
                    this.isFirstComplete="N"
                }
            },
            deep: true    //深度监听
         }
    },
    methods:{
        changeLt(lt_dat1,lt_dat2,lt_ex3){
            //alert(this.sz_ex29)
            if(this.sz_ex29=="Y"){
                return;
            }
           // alert(lt_dat1+"="+lt_dat2)
            this.lt_dat1=lt_dat1;
            this.lt_dat2=lt_dat2;
            
            let days=1;
            let hours=1;
            let hours2=0;
            let days2=1;
           
            console.log("lt_dat1====="+lt_dat1+"=="+lt_dat2)
            if(lt_dat1&&lt_dat2){
                
                let lt_dat1_=lt_dat1.substring(0,10);
                
                
                let lt_dat2_=lt_dat2.substring(0,10);
                var stime =new Date(lt_dat1).getTime();
                var etime = new Date(lt_dat2).getTime();
                var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                hours=Math.floor(usedTime/(3600*1000));
                hours2=usedTime/(3600*1000);


                let stime_ =new Date(lt_dat1_).getTime();
                var etime_ = new Date(lt_dat2_).getTime();
                var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                days=Math.floor(usedTime_/(24*3600*1000));
                days2=days
                
                //this.$emit("input",data);
                
            }
            console.log("szi_ex1====="+lt_ex3+"=="+this.nameType+"=="+this.data["item"].length)
            if(!lt_ex3){
                return;
            }
            for(let i=0;i<this.data["item"].length;i++){
                let szi_ex14=this.data["item"][i].szi_ex14;
                if(!szi_ex14){
                    szi_ex14="";
                }
                //console.log("szi_ex1==============="+this.data["item"][i].szi_ex1+"="+(this.data["item"][i].szi_ex15==lt_ex3&&(this.data["item"][i].szi_ex1.indexOf(lt_ex3)!=-1||lt_ex3.indexOf(this.data["item"][i].szi_ex1)!=-1))+"=="+(this.data["item"][i].szi_ex15.indexOf(lt_ex3)!=-1||lt_ex3.indexOf(this.data["item"][i].szi_ex15)!=-1)+"="+(this.data["item"][i].szi_ex15.indexOf(lt_ex3)))
                //console.log(((this.data["item"][i].szi_ex1==lt_ex3||(this.data["item"][i].szi_ex15==lt_ex3&&(this.data["item"][i].szi_ex1.indexOf(lt_ex3)!=-1||lt_ex3.indexOf(this.data["item"][i].szi_ex1)!=-1)))&&this.data["item"][i].szi_ex14==this.nameType)+"=="+this.nameType+"="+this.data["item"][i].szi_ex14)
                console.log("szi_ex1n====="+this.data["item"][i].szi_ex15+"="+this.data["item"][i].szi_ex1+"="+this.data["item"][i].szi_ex7)
                if((this.data["item"][i].szi_ex1==lt_ex3||(this.data["item"][i].szi_ex15==lt_ex3&&(this.data["item"][i].szi_ex7=='礼厅'||this.data["item"][i].szi_ex1.indexOf(lt_ex3)!=-1||lt_ex3.indexOf(this.data["item"][i].szi_ex1)!=-1)))&&this.nameType==szi_ex14){
                    console.log("szi_ex1========3======="+this.data["item"][i].szi_ex1+"=="+days+"=="+hours)
                    let row_=this.data["item"][i];
                    if(row_.szi_ex28=="自然天数"&&lt_dat1&&lt_dat2){
                        days++;
                        days2++;
                    }
                        
                    let lt_dat1_hour=lt_dat1.substring(11,13);
                    if(row_.szi_ex28=="12点后算半天"&&lt_dat1&&lt_dat2){
                        //  alert(lt_dat1_hour)
                        if(lt_dat1_hour>12){
                            days=days+0.5;
                        }else{
                            days++;
                        }
                        days2=days;
                    }
                    if(this.data["item"][i].szi_ex3.indexOf("时")!=-1){
                        days=hours;
                        console.log("szi_ex28======"+row_.szi_ex28)
                        if(row_.szi_ex28=="30分钟分界线，不足按0.5算"){
                            let minute=hours2-hours;
                            console.log("hours====="+hours2+"=="+hours+"=="+minute)
                            if(minute<=0.5&&minute>0){
                                days=days+0.5;
                            }else if(minute>0.5){
                                days=days+1;
                            }
                        }else if(row_.szi_ex28=='向上取整'){
                            days=Math.ceil(hours2)
                        }
                    }
                        let szi_ex2=Number(row_.szi_ex2);
                    if(this.data["item"][i].szi_ex3.indexOf("场")==-1&&this.data["item"][i].szi_ex3.indexOf("次")==-1){
                        days=days==0 ? 1:days;
                        row_.szi_ex4=days;
                        row_.szi_ex5=days*szi_ex2
                        this.$set(this.data["item"],i,row_);


                        if(this.index||(this.index=='0'||this.index==0)){
                            console.log("pId============"+this.pId+"==="+this.index)
                            this.$set(this.data[this.pId][this.index],"lt_ex12",days);
                            
                        }else{
                            
                            this.$set(this.data[this.pId],"lt_ex12",days);
                        }
                    }
                    
                    let sf_id=this.data["item"][i].szi_ex8;
                    console.log("ltSelectss==="+this.data["item"][i].szi_ex3+"="+this.data["item"][i].szi_ex1);
                    if(this.data["item"][i].szi_ex3.indexOf("场")==-1&&this.data["item"][i].szi_ex3.indexOf("次")==-1){
                        this.$post("bygex","/byg/funeral/getSfItemDp",{sf_id:sf_id}).then(data=>{
                            let names=data.name.split(",");
                            console.log("namssss=="+data.name+"=="+names.length)
                            for(let h=0;h<names.length;h++){
                                let name=names[h]
                                if(name){
                                    for(let j=0;j<this.data["item"].length;j++){
                                        let szi_ex14=this.data["item"][j].szi_ex14;
                                        if(!szi_ex14){
                                            szi_ex14="";
                                        }
                                        console.log("name============"+name+"=="+this.data["item"][j].szi_ex15+"=="+j+"=="+this.data["item"].length)
                                        if((this.data["item"][j].szi_ex1==name||(this.data["item"][j].szi_ex15==name&&(this.data["item"][j].szi_ex7=='礼厅'||this.data["item"][j].szi_ex1.indexOf(name)!=-1||name.indexOf(this.data["item"][j].szi_ex1)!=-1)))&&szi_ex14==this.nameType){
                                            console.log("data============================================================")
                                            let row__=this.data["item"][j];
                                            
                                            console.log("ss=="+row__.szi_ex1+"="+row__.szi_ex3)
                                            if(row__.szi_ex3.indexOf("场")==-1&&row__.szi_ex3.indexOf("次")==-1){
                                                let szi_ex3=row__.szi_ex3;

                                                let szi_ex2__=Number(row__.szi_ex2);
                                                if(szi_ex3.indexOf("时")!=-1){
                                                    row__.szi_ex4=hours;
                                                    row__.szi_ex5=hours*szi_ex2__
                                                }else{
                                                    row__.szi_ex4=days2;
                                                    row__.szi_ex5=days2*szi_ex2__
                                                }
                                                this.$set(this.data["item"],j,row__);
                                            }
                                        }
                                    }
                                }
                                
                            }
                        });
                    }
                    //break;  礼厅可能有多个项目需要调整天数
                }
            }
        },
        changeData(data){
            console.log("changeData==========="+data)
            let datas=JSON.parse(data);
            let vals=datas.label;
            datas.val=datas.label;
            let isTc=false;
            let id="";
            for(let item_ of this.options){
                if(item_.optname==vals){
                    if(item_.sf_ex24=="是"){
                        isTc=true;
                        id=item_.sf_id;
                    }
                }
            }

            let ids=this.id.split(".");
            let pId=ids[0];
            let lt_dat1="";
            let lt_dat2="";
            let hours=1;
            let days=0;
            //console.log("indexlt========11112222========"+this.index+"=="+(this.index==0)+"=="+(this.index=="0")+"="+(this.index!=""&&(this.index==0||this.index>0)));
            if(this.index!=""&&(this.index==0||this.index>0)){
                lt_dat1=this.data[pId][this.index]["lt_dat1"];
                lt_dat2=this.data[pId][this.index]["lt_dat2"];
            }else{
               // console.log("222222222222222222222222222222222")
                lt_dat1=this.data[pId]["lt_dat1"];
                lt_dat2=this.data[pId]["lt_dat2"];
            }
            if(lt_dat1&&lt_dat2){
                let lt_dat1_=lt_dat1.substring(0,10);
                let lt_dat2_=lt_dat2.substring(0,10);
                var stime =new Date(lt_dat1).getTime();
                var etime = new Date(lt_dat2).getTime();
                var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                hours=Math.floor(usedTime/(3600*1000));

                let stime_ =new Date(lt_dat1_).getTime();
                var etime_ = new Date(lt_dat2_).getTime();
                var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                days=Math.floor(usedTime_/(24*3600*1000));
                
            }
            console.log("hours====="+hours+"=="+days+"=="+this.nameType);
          
            if(!isTc){
                datas.addType="string";
                datas.itemType='礼厅';
                datas.nameType=this.nameType;
                datas.hour=hours;
                datas.day=days;
               
                this.$emit("funeralItemSelectEvent",JSON.stringify(datas))
                
            }else{
               
                 this.$post("bygex","/byg/funeral/getFuneralSfMealItem",{sId:id}).then(data=>{
                    
                    let isOk=true;
                    let delItem=[];
                
                    for(let i=0;i<this.data["item"].length;i++){
                        let szi_ex14=this.data["item"][i].szi_ex14;
                        if(!szi_ex14){
                            szi_ex14="";
                        }
                        if(this.data["item"][i].szi_ex15.indexOf(vals)!=-1&&this.nameType==szi_ex14){
                            isOk=false;
                        }
                        console.log(this.data["item"][i].szi_ex1+"=="+this.data["item"][i].szi_ex14+"=="+this.nameType,"nameTGypess")
                        
                        
                        if((this.data["item"][i].szi_ex16=="礼厅"&&(this.data["item"][i].szi_ex15.indexOf(vals)==-1||vals=='')&&this.nameType==szi_ex14)){
                            delItem.push(this.data["item"][i].szi_ex8)
                        }
                        
                    }

                    if(isOk){
                        if(delItem.length>0){
                            
                            for(let id of delItem){
                                let index=-1;
                                for(let i=0;i<this.data["item"].length;i++){
                                    let szi_ex14=this.data["item"][i].szi_ex14;
                                    if(!szi_ex14){
                                        szi_ex14="";
                                    }
                                    if(this.data["item"][i].szi_ex8==id&&this.nameType==szi_ex14){
                                        index=i;
                                        break;
                                    }
                                }
                                if(index>=0){
                                    this.$delete(this.data["item"],index)
                                }
                            }
                            
                        }
                        if(data.length>0){
                            for(let row_ of data){
                                row_.szi_ex13="业务";
                                row_.szi_ex15=vals;
                                if(row_.szi_ex7=="礼厅"){
                                    days=!days ? "1":days;
                                    let szi_ex2=Number(row_.szi_ex2);
                                    row_.szi_ex4=days;
                                    row_.szi_ex5=days*szi_ex2
                                }
                                if(this.nameType){
                                    row_.szi_ex14=this.nameType
                                }
                                this.$set(this.data["item"],this.data["item"].length,row_);
                            }
                        }
                        
                        

                    }else{
                         for(let i=0;i<this.data["item"].length;i++){
                            let szi_ex14=this.data["item"][i].szi_ex14;
                            if(!szi_ex14){
                                szi_ex14="";
                            }
                             if(this.data["item"][i].szi_ex7=="礼厅"&&szi_ex14==this.nameType){
                                days=!days ? "1":days;
                                let row_=this.data["item"][i];
                                let szi_ex2=Number(row_.szi_ex2);
                                row_.szi_ex4=days;
                                row_.szi_ex5=days*szi_ex2
                                
                                this.$set(this.data["item"],i,row_);
                                break;
                             }
                         }
                    }
                });
            }
            
            
           
        }
    }
}
</script>